package logic

import (
	"cartRpc/cart"
	"cartRpc/internal/svc"
	"cartRpc/models"
	"context"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGoodsLogic) CreateGoods(in *cart.CreateGoodsRequest) (*cart.CreateGoodsResponse, error) {
	// todo: add your logic here and delete this line

	var err error
	var g models.Goods

	g.Image = in.Image
	g.GoodsName = in.GoodsName
	g.GoodsPrice = float64(in.GoodsPrice)
	g.Stock = in.Stock
	g.ShopId = in.ShopId
	g.Status = in.Status

	if err = g.CreateGoods(); err != nil {
		logx.Error("商品发布失败")
		return nil, errors.New("商品发布失败")
	}
	return &cart.CreateGoodsResponse{
		GoodsId: int64(g.ID),
	}, nil
}
